package com.peng.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.peng.DictFeignClient;
import com.peng.exception.YyghException;
import com.peng.repository.HospitalRepository;
import com.peng.result.ResultCodeEnum;
import com.peng.service.HospitalService;
import com.peng.yygh.enums.DictEnum;
import com.peng.yygh.model.hosp.Hospital;
import com.peng.yygh.model.hosp.HospitalSet;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class HospitalServiceImpl implements HospitalService {

    @Autowired
    private HospitalRepository hospitalRepository;

    @Autowired
    private DictFeignClient dictFeignClient;

    @Override
    public void save(Map<String, Object> map) {
        String s = JSONObject.toJSONString(map);
        Hospital hospital = JSONObject.parseObject(s, Hospital.class);

        Hospital hoscode =hospitalRepository.getHospitalByHoscode(hospital.getHoscode());
        if (hoscode!=null){
            hospital.setStatus(hoscode.getStatus());
            hospital.setCreateTime(hoscode.getCreateTime());
            hospital.setUpdateTime(new Date());
            hospital.setIsDeleted(0);
            hospitalRepository.save(hospital);
        }else {
            hospital.setCreateTime(new Date());
            hospital.setUpdateTime(new Date());
            hospital.setIsDeleted(0);
            hospitalRepository.save(hospital);
        }
    }

    @Override
    public Hospital getByHoscode(String hoscode) {
        Hospital hospitalByHoscode =hospitalRepository.getHospitalByHoscode(hoscode);
        return hospitalByHoscode;
    }

    @Override
    public Page<Hospital> selectHospPage(Integer page, Integer limit, Hospital hospital) {
        Pageable pageable = PageRequest.of(page-1, limit);
        ExampleMatcher matching = ExampleMatcher.matching()
                .withStringMatcher(ExampleMatcher.StringMatcher.CONTAINING)
                .withIgnoreCase(true);
        Example<Hospital> example = Example.of(hospital, matching);
        Page<Hospital> hospitalPage = hospitalRepository.findAll(example, pageable);
        hospitalPage.getContent().stream().forEach(iter->{
            String hostypeString = dictFeignClient.getName(DictEnum.HOSTYPE.getDictCode(), iter.getHostype());
            String provinceString  = dictFeignClient.getName(iter.getProvinceCode());
            String cityString  = dictFeignClient.getName(iter.getCityCode());
            String districtString  = dictFeignClient.getName(iter.getDistrictCode());
            iter.getParam().put("hostypeString",hostypeString);
            iter.getParam().put("fullAddress",provinceString + cityString + districtString + iter.getAddress());
        });

        return hospitalPage;
    }

    @Override
    public void updateStatus(String id, Integer status) {
        Hospital hospital = hospitalRepository.findById(id).get();
        hospital.setStatus(status);
        hospital.setUpdateTime(new Date());
        hospitalRepository.save(hospital);
    }

    @Override
    public Map<String, Object> show(String id) {
        Hospital hospital = hospitalRepository.findById(id).get();
        String hostypeString = dictFeignClient.getName(DictEnum.HOSTYPE.getDictCode(), hospital.getHostype());
        String provinceString  = dictFeignClient.getName(hospital.getProvinceCode());
        String cityString  = dictFeignClient.getName(hospital.getCityCode());
        String districtString  = dictFeignClient.getName(hospital.getDistrictCode());
        hospital.getParam().put("hostypeString",hostypeString);
        hospital.getParam().put("fullAddress",provinceString + cityString + districtString + hospital.getAddress());
        HashMap<String, Object> map = new HashMap<>();
        //单独处理更直观
        map.put("bookingRule", hospital.getBookingRule());
        //不需要重复返回
        hospital.setBookingRule(null);
        map.put("hospital", hospital);
        return map;
    }

    @Override
    public List<Hospital> findByHosname(String hosname) {
        return hospitalRepository.findHospitalByHosnameLike(hosname);
    }

    @Override
    public Map<String, Object> item(String hoscode) {
        Map<String, Object> result = new HashMap<>();
        //医院详情
        Hospital hospital = this.setHospitalHosType(this.getByHoscode(hoscode));
        result.put("hospital", hospital);
        //预约规则
        result.put("bookingRule", hospital.getBookingRule());
        //不需要重复返回
        hospital.setBookingRule(null);
        return result;
    }


    //获取查询list集合，遍历进行医院等级封装
    private Hospital setHospitalHosType(Hospital hospital) {
        //根据dictCode和value获取医院等级名称
        String hostypeString = dictFeignClient.getName("Hostype", hospital.getHostype());
        //查询省 市  地区
        String provinceString = dictFeignClient.getName(hospital.getProvinceCode());
        String cityString = dictFeignClient.getName(hospital.getCityCode());
        String districtString = dictFeignClient.getName(hospital.getDistrictCode());

        hospital.getParam().put("fullAddress",provinceString+cityString+districtString);
        hospital.getParam().put("hostypeString",hostypeString);
        return hospital;
    }
}